<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      @change="handleTableChange"
    >
    </a-table>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { addRelationships } from '@/api/api'
let validatorCodeTimer = null

export default {
  name: 'SysPositionModal',
  mixins: [JeecgListMixin],
  components: { JDictSelectTag },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      //多选框选中的内容
      selectedRowKeys: [],
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '滤芯编号',
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: '滤芯名称',
          align: 'center',
          dataIndex: 'filterelementName',
        },
        {
          title: '可用天数',
          align: 'center',
          dataIndex: 'validity',
        },
        {
          title: '最低更换天数',
          align: 'center',
          dataIndex: 'replacementdays',
        },
      ],
      //商品类型id
      typeId: '',
      url: {
        list: '/equipment/filterelement/list',
      },
      confirmLoading: false,
    }
  },
  created() {},
  methods: {
    //表格多选框的选中事件
    // 第一个参数是选中id //第二个参数是选中对象 selectionRows
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
    },
    add(typeId) {
      this.typeId = typeId
      this.visible = true
    },
    //关闭当前对话框
    onclose() {
      //清空多选框的参数
      this.selectedRowKeys = []
      // this.$emit('onclose')
      //这个才是主要的关闭窗口
      this.visible = false
      //不知道为什么调用这个方法会报错但是调用了
      // this.$refs.form.resetFields()
    },
    handleOk() {
      const that = this
      //如果点击的数组长度不小于1就是选中了就进if 提交 如果没有就给出提示没有选中
      if (this.selectedRowKeys.length > 0) {
        var ids = ''
        //循环给id 加, 比如 id 123123,12331
        for (var i = 0; this.selectedRowKeys.length > i; i++) {
          ids += this.selectedRowKeys[i] + ','
        }
        //这个是要传入后台的对象
        var formdata = {}
        //这个是把当前选中配置滤芯的id给它
        formdata.typeId = this.typeId
        //这个是选中的id拼接后的
        formdata.filterelementId = ids
        //获取它的相应结果
        addRelationships(formdata).then(function (res) {
          if (res.success) {
            //就是一个提示
            that.$message.success(res.message)
            //如果成功就通知父组件然后 做OK事件
            that.$emit('ok')
          } else {
            //就是一个提示
            that.$message.warning(res.message)
          }
          that.onclose()
        })
      } else {
        //就是一个提示
        that.$message.warning('请选择滤芯')
      }
    },
    handleCancel() {
      this.onclose()
    },
  },
}
</script>

<style lang="less" scoped>
</style>
